<template>
	<div class="components-container">
        <div class="header-form">
            <div class="layui-inline">
                <label>火化日期从</label>
                <el-date-picker  v-model="selectData.RegcremationdateGreaterAndEquals" type="date" placeholder="输入日期"/>
                 到 <el-date-picker  v-model="selectData.RegcremationdateLessAndEquals" type="date" placeholder="输入日期"/>
            </div>
            <el-button type="primary" @click="btnSearch" :disabled="downloadLoading" :loading="this.$store.state.isloading">搜索</el-button>
            <el-button :disabled="this.$store.state.isloading || downloadLoading"  @click="btnRepeat">重置</el-button>
        </div>
        <div>
          <ul class="toolList">
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
              <el-button type="primary" :disabled="$store.state.isloading || downloadLoading" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
            <li>
            <el-button type="primary" @click="exportExcel" :disabled="this.$store.state.isloading">导出Excel</el-button>
            </li>
          </ul>
        </div>
        <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow" :hiddenColumn="hiddenColumn" :hiddenPageOrder="false" :height="height"></dataTable>
        <printmodal ref="printPdf"></printmodal>
        <h2 style="text-align:center;">{{message}}</h2>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.btn {
  text-align: center;
}
</style>
<script>
import dataTable from "@/components/Table/DataTable";
import printmodal from "@/components/Pdf/Print";
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import sweetModal from "@/components/sweetModal";

const INFO = "详情";
const EXPORT = "打印";

export default {
  name: "saleStatistics",
  mixins: [manage],
  data() {
    return {
      selectData: {
        RegcremationdateGreaterAndEquals: null,
        RegcremationdateLessAndEquals: null,
      },
      height:"620",
      downloadLoading:false,
      OutDataExcel:[],
      chargetype: [],
      title: null,
      order: null,
      message: null,
      hiddenColumn: false
    };
  },
  methods: {
    exportExcel() {
      var startTime;
      var endTime;
      if(this.selectData.RegcremationdateGreaterAndEquals){
        startTime = this.selectData.RegcremationdateGreaterAndEquals.toLocaleDateString();
      }else{
        startTime = '空';
      }
      if(this.selectData.RegcremationdateLessAndEquals){
        endTime = this.selectData.RegcremationdateLessAndEquals.toLocaleDateString();
      }else{
        endTime = '空';
      }
      var header = '惠民政策减免汇总表' + '(' + startTime + '--' + endTime + ')';
       if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.OutDataExcel, header, undefined,"hmzcjm");
        this.downloadLoading = false;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      this.dataRepeat();
      switch (this.order) {
        case EXPORT:
          this.$refs.printPdf.report(PRINT.PDFREDUCTIONSTATISTICS,this.getDataC());
          break;
        default:
      }
    },
    getColumns() {
      this.headList = [
            {ColumnAlign: "center",ColumnKey: "DTRNAME",ColumnName: "项目（一）",ColumnOrder: "DTRNAME",},
            {ColumnAlign: "center",ColumnKey: "Conname",ColumnName: "项目（二）",ColumnOrder: "Conname",},
            {ColumnAlign: "center",ColumnKey: "Num",ColumnName: "人数（具）",ColumnOrder: "Num",},
            {ColumnAlign: "center",ColumnKey: "Money",ColumnName: "金额（元）",ColumnOrder: "Money",}
        ];
    },
    getDataC(){
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.RegcremationdateGreaterAndEquals = formatDate(
        Data.RegcremationdateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.RegcremationdateLessAndEquals = formatDate(
        Data.RegcremationdateLessAndEquals,
        "yyyyMMdd"
      );
      let data = {
        PageIndex: 1,
        PageSize: 100,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      return data;
    },
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.RegcremationdateGreaterAndEquals = formatDate(
        Data.RegcremationdateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.RegcremationdateLessAndEquals = formatDate(
        Data.RegcremationdateLessAndEquals,
        "yyyyMMdd"
      );
      let data = {
        PageIndex: 1,
        PageSize: 100,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Report.QueryReductionStatistics,
        res => {
          this.dataList = res.data.content.OutData;
          this.OutDataExcel = res.data.content.OutDataExcel;
          this.message = res.data.content.Message;
          this.pageOrder.totalNum = res.data.content.RecordCount;
        }
      );
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {};
      this.selectData.RegcremationdateGreaterAndEquals = new Date();
      this.selectData.RegcremationdateGreaterAndEquals.setDate(1);
      this.selectData.RegcremationdateLessAndEquals = new Date();
      var day = new Date();
      var year = day.getFullYear();
      var month = day.getMonth()+1;
      var d = new Date(year, month, 0);
      var dat = d.getDate();
      this.selectData.RegcremationdateLessAndEquals.setDate(dat);
      this.getData();
    },
    btnShow(btn){
      switch (btn) {
          case EXPORT:
            return true;
            break;
          default: return false;
        }
    },
    //数据重置
    dataRepeat() {}
  },
  mounted() {},
  components: {
    dataTable,
    sweetModal,
    printmodal
  },
  //当前选中行
  selectRow(row) {
    this.currentRow = row;
    this.$emit("selectRow", row, "");
  },
  created() {
    this.selectData.RegcremationdateGreaterAndEquals = new Date();
    this.selectData.RegcremationdateGreaterAndEquals.setDate(1);
    this.selectData.RegcremationdateLessAndEquals = new Date();
    var day = new Date();
    var year = day.getFullYear();
    var month = day.getMonth()+1;
    var d = new Date(year, month, 0);
    var dat = d.getDate();
    this.selectData.RegcremationdateLessAndEquals.setDate(dat);
    this.getColumns();
    this.getData();
  }
};
</script>